const config = require('../../config.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollHeight:"",
    showAddress:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var userInfo = getApp().globalData.userInfo;
    var that = this;
    that.setData({
      scrollHeight: wx.getSystemInfoSync().windowHeight - 50
    });

   wx.request({
     url: config.requestUrl +'/accountAddress/findByEffectiveAddress',
     header: {"token":userInfo.xOpenid},
     success(res){
       if(res.data.data!=null){
         that.setData({"address":res.data.data,showAddress:true});
       };
     }
   })

    wx.request({
      url: config.requestUrl + '/order/getCartInfo',
      header: { "token": userInfo.xOpenid },
      success(res) {
        var totalPrice = 0;
        var totalAmount=0;
        for (var i = 0; i < res.data.data.length; i++) {
          var cartInfo = res.data.data[i];
          if (cartInfo.status == 1) {
            totalPrice = totalPrice + cartInfo.price * cartInfo.amount;
            totalAmount = totalAmount+cartInfo.amount;
          }
        }
        that.setData({ "cartInfos": res.data.data, "totalPrice": totalPrice, "totalAmount":totalAmount});
      }
    });
  },
  toChooseAddress:function(){
    wx.navigateTo({
      url: '/pages/chooseAddress/chooseAddress',
    })
  },
  createOrder: function () {
    var userInfo = getApp().globalData.userInfo;
    wx.request({
      url: config.requestUrl + '/order/createOrder',
      header: { "token": userInfo.xOpenid },
      success(res) {
        if(res.data.code==1){
          wx.switchTab({
            url: '/pages/orderStatus/orderStatus'
          })
        }else{
          wx.showToast({
            title: res.data.message,
            icon: 'success',
            duration: 2000
          })
        }
    }});
  
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})